package com.woniuxy.carlife.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.woniuxy.carlife.domain.ServiceCategory;
import com.woniuxy.carlife.dto.ServiceCategoryDto;
import com.woniuxy.carlife.mapper.ServiceCategoryMapper;
import com.woniuxy.carlife.param.SaveServiceCategoryParam;
import com.woniuxy.carlife.service.ServiceCategoryService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import javax.annotation.Resource;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * @author fx
 * @ClassName:ServiceCategoryServiceImpl
 * @Description: 服务类别service实现类
 * @date 2021/4/6 20:00
 **/

@Service
public class ServiceCategoryServiceImpl extends ServiceImpl<ServiceCategoryMapper, ServiceCategory> implements ServiceCategoryService {

    @Resource
    private ServiceCategoryMapper mapper;

    @Resource
    private RedisTemplate redisTemplate;

    /**
     * 功能描述〈添加服务类别〉
     * @author:fx
     * @param: @param null:
     * @return:
     * @Date 2021/4/10 16:17
     * @throws
     */
    @Override
    public int saveServiceCatrgory(SaveServiceCategoryParam  categoryParam) {
        //先根据名称查redis
        String s =(String) redisTemplate.opsForValue()
                .get("com.woniuxy:carlife:serviceproject:servicecagetory:name:" + categoryParam.getServiceCategoryName());
        if (ObjectUtils.isEmpty(s)){
            //根据名字查数据库
            ServiceCategory coupon_category = mapper.selectOne(new QueryWrapper<ServiceCategory>()
                    .eq("service_category_name", categoryParam.getServiceCategoryName()));
            if (!ObjectUtils.isEmpty(coupon_category)){
                //如果数据库有数据就先把这个优惠券类别名存到redis
                redisTemplate.opsForValue()
                        .set("com.woniuxy:carlife:serviceproject:servicecagetory:name:" + categoryParam.getServiceCategoryName(),
                                categoryParam.getServiceCategoryName());
                //设置过期时间,5分钟
                redisTemplate.expire("com.woniuxy:carlife:serviceproject:servicecagetory:name:" + categoryParam.getServiceCategoryName(),
                        5*60, TimeUnit.SECONDS);
                return -1;
            }
        }else{
            return -1;
        }
        //创建服务类别对象
        ServiceCategory serviceCategory = new ServiceCategory();
        //类别名称
        serviceCategory.setServiceCategoryName(categoryParam.getServiceCategoryName());
        //类别等级
        serviceCategory.setServiceCategoryLevel(categoryParam.getServiceCategoryLevel());
        //该类别上级类别id
        serviceCategory.setServiceCategorySuperior(categoryParam.getServiceCategorySuperior());
        //类别状态,默认为使用中
        serviceCategory.setCategoryStatus("使用中");
        //类别创建时间
        serviceCategory.setCategoryCreateTime(new Timestamp(new Date().getTime()));
        //存储到数据库
        int insert = mapper.insert(serviceCategory);
        if (insert>0){
            return 1;
        }
        return 0;
    }

    /**
     * 功能描述〈查询所有类别状态为已使用的,并按类别等级分类关联〉
     * @author:fx
     * @param: @param null:
     * @return:
     * @Date 2021/4/10 17:08
     * @throws
     */
    @Override
    public List<ServiceCategoryDto> findAllServiceCategory() {
        //先查询所有服务类别
        List<ServiceCategoryDto> serviceCategoryDto = mapper.findServiceCategoryDto();
        //创建一个一级类别集合
        ArrayList<ServiceCategoryDto> categoryOne = new ArrayList<>();
        //循环遍历
        for (ServiceCategoryDto s : serviceCategoryDto) {
            if (s.getServiceCategoryLevel()==1){
                //创建一个二级类别集合
                s.setCategoryTwo(new ArrayList<ServiceCategoryDto>());
                //把是一级的类别装进去
                categoryOne.add(s);
            }
        }

        //声明一个二级类别集合
        ArrayList<ServiceCategoryDto> categoryTwo = new ArrayList<ServiceCategoryDto>();
        //再循环一次找二级类别
        for (ServiceCategoryDto st : serviceCategoryDto) {
            //再循环装一级类别的集合
            for (ServiceCategoryDto co : categoryOne) {
                //如果这个对象的上级类别和一级类别对应上了，加添加进一级类别对象的二级类别属性中
                if (st.getServiceCategorySuperior()==co.getServiceCategoryId()){
                    co.getCategoryTwo().add(st);
                    //给二级类别属性添加三级类别集合
                    st.setCategoryThree(new ArrayList<ServiceCategoryDto>());
                    //把这个对象添加进二级类别集合中
                    categoryTwo.add(st);
                }
            }
        }

        //再循环一次找三级类别
        for (ServiceCategoryDto ct : categoryOne) {
            //循环二级类别集合
            for (ServiceCategoryDto ctw : categoryTwo) {
                //如果是二级类别对应的三级类别，就添加到对应的二级类别集合中
                if (ct.getServiceCategorySuperior()==ctw.getServiceCategoryId()){
                    ctw.getCategoryThree().add(ct);
                }
            }
        }
        return categoryOne;
    }

    /**
     * 功能描述〈根据id修改类别状态为停用〉
     * @author:fx
     * @param: @param null:
     * @return:
     * @Date 2021/4/12 9:56
     * @throws
     */
    @Override
    public boolean updateServiceCategoryStatusByCategoryId(Integer categoryId) {
        ServiceCategory serviceCategory = new ServiceCategory();
        serviceCategory.setServiceCategoryId(categoryId);
        serviceCategory.setCategoryStatus("停用");
        int i = mapper.updateById(serviceCategory);
        if (i>0){
            return true;
        }
        return false;
    }
}
